package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"socials/pkg"
)

func JWtpPare() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("token")

		if token == "" {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": 400,
				"msg":  "token为空",
			})
			c.Abort()
			return
		}

		userId, err := pkg.GetToken(token)

		if err != "" {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": 400,
				"msg":  "token解析失败",
			})
			c.Set(userId, token)
			c.Next()
		}
	}
}
